<?php


namespace app\zhongcan\controller;


use api\web\model\BannerModel;

use app\zhongcan\validate\NewsVaildate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class BannerController extends AdminBaseController
{



    public function index(){
        $list = BannerModel::order('list_order', 'desc')
            ->paginate(3);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }

    public function add(){
        $list = BannerModel::select();

        return $this->fetch('add',[
            'list'=>$list,
        ]);
    }
    public function addpost(){
        $data = input('post.');

        $validate = new NewsVaildate();
        $result = $validate->scene('edit3')->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        if (BannerModel::create($data)) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $list = BannerModel::find($id);
        return $this->fetch('edit',['list' => $list]);
    }

    public function editPost(){

        $data = input('post.');
        $validate = new NewsVaildate();
        $result = $validate->scene('edit3')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        $list = BannerModel::find($data['id']);
        if(!$list){
            $this->error('未查询的记录');
        }
        if($list->update($data)){
            $this->success('保存成功','Banner/index');
        }else{
            $this->error('保存失败');
        }
    }

    public function del()
    {
        $id = input('id/d');
        $news = BannerModel::destroy($id);
        if ($news) {
            $this->success('删除成功','Banner/index');
        } else {
            $this->success('删除失败');
        }
    }
    public function order()
    {
        parent::listOrders(new BannerModel());
        $this->success('排序成功');
    }
}